/**
 *
 */
package com.naiterui.ehp.bs.patient.controller;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.context.ResourceHolder;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bp.support.security.SettingConfig;
import com.naiterui.ehp.bs.patient.exception.ExceptionCodes;
import com.naiterui.ehp.bs.patient.service.IPatientFamilyService;
import com.naiterui.ehp.bs.patient.service.ISendSMSService;
import com.naiterui.ehp.bs.patient.utils.constants.PatientConstant;
import com.naiterui.ehp.bs.patient.utils.exception.PatientBusinessException;
import com.naiterui.ehp.bs.patient.vo.family.FamilyInitVO;
import com.naiterui.ehp.bs.patient.vo.family.FamilyLiteVO;
import com.naiterui.ehp.bs.patient.vo.family.FamilyVO;
import com.naiterui.ehp.bs.patient.vo.family.param.FamilyParamVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 患者家庭成员controller
 * @since 2.18
 */
@Api(tags = {SwaggerTag.PATIENT + SwaggerTag.FAMILY_MANAGE})
@RequestMapping("ap/patient/family")
@RestController
public class ApPatientFamilyController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ApPatientFamilyController.class);
    @Autowired
    private IPatientFamilyService patientFamilyService;

    @Autowired
    private ISendSMSService sendSMSService;

    /**
     * 获取家庭成员列表
     * PatientFamilyController.list()
     * @Date 2018年5月28日
     * @since 2.18
     * @param patientId
     * @return
     * @throws BusinessException
     */
    @ResponseBody
    @RequestMapping("list")
    @ApiOperation(value = "获取患者家庭成员列表", notes = "获取患者家庭成员列表", httpMethod = "POST")
    @ApiImplicitParams({@ApiImplicitParam(name = "patientId", value = "患者ID", required = true, dataType = "long", paramType = "query")})
    public ResponseVO<List<FamilyLiteVO>> list(Long patientId) throws BusinessException {
        if (null == patientId) {
            patientId = (Long) ResourceHolder.getResource(SettingConfig.PATIENT_ID);
        }
        if (patientId == null || patientId < 0) {
            LOGGER.error("获取家庭成员列表参数错误 : patientId: {}", patientId);
            throw new PatientBusinessException(ExceptionCodes.PARAM_ERROR);
        }
        List<FamilyLiteVO> familyList = patientFamilyService.getFamilyListByPatientId(patientId);
        if (familyList == null || familyList.size() == 0) {
            FamilyVO seftInfo = patientFamilyService.saveAndGetSelfInfo(patientId);
            if (seftInfo != null) {
                FamilyLiteVO liteVO = new FamilyLiteVO();
                BeanUtils.copyProperties(seftInfo, liteVO);
                familyList.add(liteVO);
            }
        }
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS, familyList);
    }

    /**
     * 獲取家庭成員信息
     * PatientFamilyController.detail()
     * @Date 2018年5月28日
     * @since 2.16
     * @param patientId
     * @param familyId
     * @return
     * @throws BusinessException
     */
    @ResponseBody
    @RequestMapping("detail")
    @ApiOperation(value = "获取家庭成員詳情", notes = "获取家庭成員詳情", httpMethod = "POST")
    @ApiImplicitParams({@ApiImplicitParam(name = "patientId", value = "患者ID", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "familyId", value = "家庭成員ID", required = true, dataType = "long", paramType = "query")})
    public ResponseVO<FamilyVO> detail(Long patientId, Long familyId) throws BusinessException {
        if (null == patientId) {
            patientId = (Long) ResourceHolder.getResource(SettingConfig.PATIENT_ID);
        }
        if (patientId == null || patientId < 0 || familyId == null) {
            LOGGER.error("获取家庭成员详情参数错误 : patientId: {},familyId:{}", patientId, familyId);
            throw new PatientBusinessException(ExceptionCodes.PARAM_ERROR);
        }
        FamilyVO vo1 = patientFamilyService.getFamilyById(familyId, patientId);

        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS, vo1);
    }

    /**
     * 初始化家庭成员信息
     * PatientFamilyController.init()
     * @Date 2018年5月28日
     * @since 2.18
     * @param patientId
     * @return
     * @throws BusinessException
     */
    @ResponseBody
    @RequestMapping("init")
    @ApiOperation(value = "家庭成員初始化", notes = "家庭成員初始化", httpMethod = "POST")
    @ApiImplicitParams({@ApiImplicitParam(name = "patientId", value = "患者ID", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "操作类型:0-添加患者病例；1-找医生", dataType = "long", paramType = "query")})
    public ResponseVO<FamilyInitVO> init(Long patientId, Integer type) throws BusinessException {
        if (null == patientId) {
            patientId = (Long) ResourceHolder.getResource(SettingConfig.PATIENT_ID);
        }
        if (patientId == null || patientId < 0) {
            LOGGER.error("家庭成员初始化参数错误 : patientId: {}", patientId);
            throw new PatientBusinessException(ExceptionCodes.PARAM_ERROR);
        }

        FamilyVO selfInfo = null;
        if (type != null && PatientConstant.FAMILY_INIT_TYPE_CONSULTROOM == type) {
            selfInfo = patientFamilyService.getSelfOrBaseInfo(patientId);
        } else {
            selfInfo = patientFamilyService.saveAndGetSelfInfo(patientId);
        }
        FamilyInitVO initVO = new FamilyInitVO();
        if (selfInfo != null) {
            initVO.setHasSelf(true);
            initVO.setSelfInfo(selfInfo);
        } else {
            initVO.setHasSelf(false);
            initVO.setSelfInfo(new FamilyVO());
        }

        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS, initVO);
    }

    /**
     * 添加家庭成員
     * PatientFamilyController.insert()
     * @Date 2018年5月28日
     * @since 2.18
     * @param patientId
     * @return
     * @throws BusinessException
     */
    @PostMapping("insert")
    @ApiOperation(value = "新增家庭成員", notes = "新增家庭成員", httpMethod = "POST")
    public ResponseVO<Void> insert(Long patientId, FamilyParamVO param) throws BusinessException {
        if (null == patientId) {
            patientId = (Long) ResourceHolder.getResource(SettingConfig.PATIENT_ID);
        }
        LOGGER.info("添加家庭成员：patientId={}, param={}", patientId, param);
        //短信校验
        sendSMSService.validateMsg(patientId, param.getPhone(), param.getActionType(), param.getCode());
        patientFamilyService.insertFamily(patientId, param);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    /**
     * 家庭健康档案开关
     * @return
     * @throws BusinessException
     */
    @PostMapping("visible/set")
    @ApiOperation(value = "家庭健康档案开关", notes = "家庭健康档案开关", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "familyId", value = "家庭成員ID", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "visible", value = "可见状态：1：私密，2：可见", required = true, dataType = "int", paramType = "query")
    })
    public ResponseVO<Void> visibleSet(Long familyId, Integer visible) throws BusinessException {
        patientFamilyService.setVisible(familyId, visible);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }


    /**
     * 更新家庭成员
     * PatientFamilyController.update()
     * @Date 2018年5月28日
     * @since 2.16
     * @param patientId
     * @param param
     * @return
     * @throws BusinessException
     */
    @ResponseBody
    @RequestMapping("update")
    @ApiOperation(value = "更新家庭成員", notes = "更新家庭成員", httpMethod = "POST")
    @ApiImplicitParams({@ApiImplicitParam(name = "patientId", value = "患者ID", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "familyId", value = "家庭成員ID", required = true, dataType = "long", paramType = "query")})
    public ResponseVO<Void> update(Long patientId, Long familyId, FamilyParamVO param) throws BusinessException {
        if (null == patientId) {
            patientId = (Long) ResourceHolder.getResource(SettingConfig.PATIENT_ID);
        }
        if (patientId == null || patientId < 0 || familyId == null || param == null) {
            LOGGER.error("修改家庭成員参数错误 : patientId: {},familyId : {},paramVO: {}", patientId, familyId, param);
            throw new PatientBusinessException(ExceptionCodes.PARAM_ERROR);
        }
        //短信校验
        sendSMSService.validateMsg(patientId, param.getPhone(), param.getActionType(), param.getCode());
        patientFamilyService.updateFamily(patientId, familyId, param);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }


}